/*
 * @Author: xmily
 * @Date: 2021-11-09 22:32:30
 * @LastEditors: xmily
 * @LastEditTime: 2023-05-09 19:46:19
 * @FilePath: \glxxapi\routes\wechat\afterclassrv.js
 * @Description:
 *
 * Copyright (c) 2022 by xmily/mxthink.com.cn, All Rights Reserved.
 */
const router = require("koa-router")();
const aftclassrv = require("../../models/AfterClassService/application.js");
const aftclassrv_lesson = require("../../models/AfterClassService/lesson.js");
const aftclassrv_attendance = require("../../models/AfterClassService/attendance.js");
const aftclassrv_activitcode = require("../../models/AfterClassService/activitCode.js");
const aftclassrv_addstatus=require("../../models/AfterClassService/addstatus.js");
const commMethod=require('../../cfg/commMethod.js')
const sitecfg = require("../../cfg/siteconfig.js");
router.prefix("/afterclasssrv");


var term=""
var termlist=[]
router.use(async (ctx, next) => {
  termlist = await commMethod.getTermList();
  term = termlist.sort()[termlist.length - 1];
  await next();
});


//获取课后服务增删查改开放状态
router.get("/getCRUDStatus",async(ctx,next)=>{
  let termlist=await commMethod.getTermList();  
  let term=ctx.query.term||termlist.sort()[termlist.length-1]
  try{
      let result=await aftclassrv_addstatus.GetByQuery({"term":term,"month":ctx.query.month})
      ctx.body={'error':false,result:result}
  }catch(err){
      throw new Error("获取课后服务增删查改开放状态数据时出错:[" + err + "]")
  }
})

//获取学有优教APP学生激活码
router.get("/getactivitcode", async (ctx, next) => {
  try {
    let userinfo = ctx.request.token;
    let res=null
    if (userinfo.accountType == "教师") {
      res= await aftclassrv_activitcode.GetByQuery({
        姓名: userinfo.accountname,
      });
    } else {
      res = await aftclassrv_activitcode.GetByQuery({
        姓名: userinfo.accountname,
        班级: "小学" + userinfo.classno,
      });
    }
    if (res) {
      ctx.body = { error: false, result: res };
    } else {
      ctx.body = { error: false, result: { 激活码: null } };
    }
  } catch (err) {
    ctx.body = { error: true, message: err };
  }
});

router.post("/saveapplication", async (ctx, next) => {
  try {
    //保存课后服务申请
    let userinfo = ctx.request.token;
    let interestinfo = ctx.request.body.interestinfo;
    interestinfo.classno = userinfo.classno;
    interestinfo.stuname = userinfo.accountname;
    interestinfo.stuid = userinfo.userid;
    interestinfo.term = term;
    interestinfo.schoolname = sitecfg.schoolname;
    interestinfo.status = ctx.state.aftclssvrStatus.get("unAllot");
    let applicationinfo = await aftclassrv.Save(interestinfo);
    ctx.body = { error: false, result: applicationinfo };
  } catch (err) {
    throw new Error("保存时出错:[" + err + "]");
  }
});

router.post("/updateapplication", async (ctx, next) => {
  try {
    //保存课后服务申请
    let userinfo = ctx.request.token;
    let interestinfo = ctx.request.body.interestinfo;
    let applicationinfo = await aftclassrv.Save(interestinfo);
    ctx.body = { error: false, result: applicationinfo };
  } catch (err) {
    throw new Error("保存时出错:[" + err + "]");
  }
});

router.post("/delapplication", async (ctx, next) => {
  let query = ctx.request.body.query;
  let token = ctx.request.token;
  query.stuid = token.userid;
  try {
    let applicationinfo = await aftclassrv.Del(query);
    ctx.body = { error: false, result: applicationinfo };
  } catch (err) {
    throw new Error("删除时出错:[" + err + "]");
  }
});

//获取基础课考勤记录
router.post("/getBaseLessonAttendance", async (ctx, next) => {
  let query = ctx.request.body.query;
  let token = ctx.request.token;
  try {
    let attendanceList = await aftclassrv_attendance.myFindByQuery(query, null);
    ctx.body = { error: false, result: attendanceList };
  } catch (err) {
    throw new Error("获取基础课考勤记录时出错:[" + err + "]");
  }
});

router.get("/getInterest", async (ctx, next) => {
  let token = ctx.request.token;

  let termlist=await commMethod.getTermList();  
  let term=ctx.query.term||termlist.sort()[termlist.length-1]
  try {
    let res = await aftclassrv.GetByQuery(
      {
        stuid: token.userid,
        schoolname: sitecfg.schoolname,
        term: term,
      },
      { applicationwrite: 0, safewrite: 0 }
    );
    ctx.body = { error: false, result: res };
  } catch (err) {
    throw new Error("获取用户兴趣班信息时出错:[" + err + "]");
  }
});

router.get("/getOneInterest", async (ctx, next) => {
  let token = ctx.request.token;
  let id = ctx.query.id;
  try {
    let res = await aftclassrv.GetOneByQuery({
      _id: id,
    });
    ctx.body = { error: false, result: res };
  } catch (err) {
    throw new Error("获取用户兴趣班信息时出错:[" + err + "]");
  }
});

router.get("/getlesson", async (ctx, next) => {
  try {
    let res = await aftclassrv_lesson.GetByQuery(
      {
        schoolname: sitecfg.schoolname,
        term: term,
        type:"interest"
      },
      { lessonname: 1, _id: 0, limit: 1, teacher: 1 }
    );
    console.log(res);
    ctx.body = { error: false, result: res };
  } catch (err) {
    throw new Error("获取课后服务社团课程时出错:[" + err + "]");
  }
});
//保存or更新考勤
router.post("/saveAttendance", async (ctx, next) => {
  let attendance = ctx.request.body.attendance;
  let token = ctx.request.token;
  attendance.term = term;
  attendance.schoolname = sitecfg.schoolname;
  try {
    let res = await aftclassrv_attendance.Save(attendance);
    ctx.body = { error: false, result: res };
  } catch (err) {
    throw new Error("保存考勤数据时出错:[" + err + "]");
  }
});

module.exports = router;
